package com.caird.pmp.iot.message.impl;

import com.caird.pmp.iot.message.MessageBase;
import io.netty.buffer.ByteBuf;
import lombok.Data;

@Data
public class ServerMessage8001 extends MessageBase {

    public static final byte SUCCESS = 0;       //成功/确认
    public static final byte FAILURE = 1;       //失败
    public static final byte ERROR = 2;         //消息有误
//    public static final byte UNSUPPORTED = 3;   //不支持
//    public static final byte ALARM_PROCESS_ACK = 4;//报警处理确认


    public ServerMessage8001() {
        setId((short)0x8001);
    }

    @Override
    public int getMaxBodyLength() {
        return 5;
    }

    @Override
    public boolean encodeBody(ByteBuf raw) {
        if (raw.writableBytes() < 5)
            return false;

        //应答流水号
        raw.writeShort(resFlowNo);

        //应答ID
        raw.writeShort(resId);

        //结果码
        raw.writeByte(result);

        return true;
    }

    public static ServerMessage8001 make(MessageBase dm, short flowNo, byte result) {
        ServerMessage8001 sm = new ServerMessage8001();
        sm.setSimNo(dm.getSimNo());
        sm.setFlowNo(flowNo);
        sm.setResFlowNo(dm.getFlowNo());
        sm.setResId(dm.getId());
        sm.setResult(result);
        return sm;
    }

    public static ServerMessage8001 success(MessageBase dm, short flowNo) {
        return make(dm, flowNo, SUCCESS);
    }

    public static ServerMessage8001 failure(MessageBase dm, short flowNo) {
        return make(dm, flowNo, FAILURE);
    }

    public static ServerMessage8001 error(MessageBase dm, short flowNo) {
        return make(dm, flowNo, ERROR);
    }

    //应答流水号
    private short resFlowNo;

    //应答ID
    private short resId;

    //结果
    private byte result;

}
